package com.itcast.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcast.constant.MessageConstant;
import com.itcast.entity.Result;
import com.itcast.pojo.OrderSetting;
import com.itcast.service.OrdersettingService;
import com.itcast.utils.POIUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.util.*;

@RestController
@RequestMapping("/ordersetting")
public class OrdersettingController {

    @Reference
    private OrdersettingService ordersettingService;

    /**
     * 上传文件
     * @param excelFile
     * @return 返回上传是否成功
     */
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        try {
            List<String[]> readExcel = POIUtils.readExcel(excelFile);
            if (readExcel!=null && readExcel.size()>0){
                for (String[] strings : readExcel) {
                    if (strings!=null&&strings.length>0){
                        String dateString = strings[0];
                        String cell = strings[1];
                        try {
                            ordersettingService.add(dateString,cell);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            return new Result
                    (true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
        return new Result
                (false,MessageConstant.IMPORT_ORDERSETTING_FALL);
        }
    }

    /**
     * 查询所有预约信息
     * @param StrMonth
     * @return
     */

    @RequestMapping("/findAll")
    public Result findAll(String StrMonth){
        try {
            List<Map<String, Object>> list
                    = ordersettingService.findAll(StrMonth);
            return new Result
                    (true, MessageConstant.QUERY_ORDER_SUCCESS, list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result
                    (false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**
     *     修改预约人数
     * @param orderSetting
     * @return
     */
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    @RequestMapping("/editnumber")
    public Result editNumber(@RequestBody OrderSetting orderSetting){

        Result result = null;
        try {

             result = ordersettingService.editNumber(orderSetting);
            return  result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;

    }
}
